<template>
    <div class="table">
        <div class="divSearch">
            <!--搜索框-->
            <Row>
                <Col :lg="{span:5}" :md="{span:8}" :sm="{span:16}" :xs="{span:22}">
                    <label class="searchLabel">填报年度:</label>
                    <Select v-model="searchModel.FillInYear" placeholder="全部" :label-in-value="true" @on-change="GetData()" class="searchInput">
                        <Option :value="item.DictText" v-for="item in CreateYearList" :key="item.DictValue">{{item.DictText}}</Option>
                    </Select>
                </Col>
                <Col :lg="{span:2}" :md="{span:2}" :sm="{span:2}" :xs="{span:2}" v-show="isFull">
                    <Button icon="ios-search" type="primary" @click="GetData()" class="searchButton">查询</Button>
                </Col>
                <Col :lg="{span:2}" :md="{span:2}" :sm="{span:2}" :xs="{span:2}" style="float:right">
                    <Button v-show="DataModel.ReportStatus>1" icon="ios-download" type="primary" @click="ExcelDown()" class="searchButton">数据导出</Button>
                </Col> 
                <Col :lg="{span:2}" :md="{span:2}" :sm="{span:2}" :xs="{span:2}" style="float:right" v-show="DataModel.ReportStatus<3&&isFull">
                    <Upload :on-success="handleSuccess" :action="GetPostUrl()">
                        <Button type="primary" icon="ios-cloud-upload-outline" class="searchButton">数据导入</Button>
                    </Upload>
                </Col>
                <Col :lg="{span:2}" :md="{span:2}" :sm="{span:2}" :xs="{span:2}"  style="float:right">
                    <Button icon="ios-cloud-download" type="primary" @click="TmpDown()" class="searchButton">模板下载</Button>
                </Col>
            </Row>
        </div>
        <Row>
            <Col>
                <div v-if="isFull">
                    <Form ref="DataModel" :model="DataModel" :rules="ruleModel" >  
                        <table class="reportTable">
                            <caption>
                                <label style="float:left">(五){{searchModel.FillInYear}}蔬菜外销调查表(全面统计)——<span class="Tip">单位(吨、元)</span></label>
                                <label style="float:right">当前状态:
                                    <span v-if="DataModel.ReportStatus==0" class="Error">退回重报</span> 
                                    <span v-else-if="DataModel.ReportStatus==1" class="NoReport">未填写</span>
                                    <span v-else-if="DataModel.ReportStatus==2" class="AlreadyReport">已填写</span>
                                    <span v-else-if="DataModel.ReportStatus==3" class="UpReport">已上报</span>
                                    <span v-else-if="DataModel.ReportStatus==4" class="Success">审核通过</span>  
                                    <span v-else class="NoReport">无需填报</span>               
                                </label>
                            </caption>
                            <thead>
                                <tr>
                                    <td colspan="2">全年蔬菜总产量(吨)</td>
                                    <td colspan="2">蔬菜产值(元)</td>
                                </tr>
                                <tr>
                                    <td>总量</td>
                                    <td>外销量(<span class="Tip">出县</span>)</td>
                                    <!-- <td>上年同期数</td> -->
                                    <td>总产值</td>
                                    <td>外销产值(<span class="Tip">出县</span>)</td>
                                    <!-- <td>上年同期数</td> -->
                                </tr>
                                <!-- <tr>
                                    <td rowspan="2">蔬菜全年生产总量</td>
                                    <td colspan="2">其中外销量(出县)</td>
                                    <td rowspan="2">蔬菜全年生产总产值</td>
                                    <td colspan="2">其中外销产值(出县)</td>
                                </tr>
                                <tr>
                                    <td>蔬菜外销量(出县)</td>
                                    <td>比上年增减%</td>
                                    <td>蔬菜外销产值(出县)</td>
                                    <td>比上年增减%</td>
                                </tr> -->
                            </thead>

                            <tr>   
                                        
                                <td>
                                    <Poptip trigger="focus"> 
                                        <Form-item prop="ProductNumber">  
                                            <Input v-model="DataModel.ProductNumber"></Input>
                                        </Form-item>
                                        <span slot="content">
                                            上年数据:{{DataModel.OldProductNumber==null||""?"暂无":DataModel.OldProductNumber}}
                                            ——
                                            增减比:{{getScale(DataModel.OldProductNumber,DataModel.ProductNumber)}}
                                        </span>
                                    </Poptip>
                                </td>
                                <td>
                                    <Poptip trigger="focus" > 
                                        <Form-item prop="SaleNumber">                                
                                            <Input v-model="DataModel.SaleNumber"></Input>
                                        </Form-item>
                                        <span slot="content">
                                            上年数据:{{DataModel.OldSaleNumber==null||""?"暂无":DataModel.OldSaleNumber}}
                                            ——
                                            增减比:{{getScale(DataModel.OldSaleNumber,DataModel.SaleNumber)}}
                                        </span>
                                    </Poptip>
                                </td>
                                <!-- <td>生产量增减比</td> -->
                                <td>
                                    <Poptip trigger="focus" > 
                                        <Form-item prop="ProductMoney">                          
                                            <Input v-model="DataModel.ProductMoney"></Input>
                                        </Form-item>
                                        <span slot="content">
                                            上年数据:{{DataModel.OldProductMoney==null||""?"暂无":DataModel.OldProductMoney}}
                                            ——
                                            增减比:{{getScale(DataModel.OldProductMoney,DataModel.ProductMoney)}}
                                        </span>
                                    </Poptip>
                                </td>                                                     
                                <td>
                                    <Poptip trigger="focus">  
                                        <Form-item prop="SaleMoney">
                                            <Input v-model="DataModel.SaleMoney"></Input>
                                        </Form-item>
                                        <span slot="content">
                                            上年数据:{{DataModel.OldSaleMoney==null||""?"暂无":DataModel.OldSaleMoney}}
                                            ——
                                            增减比:{{getScale(DataModel.OldSaleMoney,DataModel.SaleMoney)}}
                                        </span>
                                    </Poptip>
                                </td>
                                <!-- <td>生产值增减比</td> -->
                            </tr>
                        
                        </table> 
                    </Form>
                    <div class="divButton">
                        <Button type="primary" @click="save('DataModel')" :disabled="DataModel.ReportStatus < 3 && BtnSaveStatus ? false:true">保存</Button>
                        <Button type="primary" @click="DataUp(DataModel.ReportId)" :disabled="DataModel.ReportStatus == 2 ? false:true">上报</Button>
                    </div>
                </div>
                <div class="FIllTip" v-else>温馨提示:本时段不在系统报表填报时间内，请耐心等待。</div>
                <Alert class="alert" banner type="error">
                    <p>填报时间：每年1月10日</p>
                    <p>1.全省范围内从乡（镇）起，逐级全面统计，由县级填报经州（市）审核后上报。</p>
                    <p>2.蔬菜外销量外销：指当地蔬菜调出县域范围以外的蔬菜量。</p>
                    <span slot="desc"></span>
                </Alert>
            </Col>
        </Row>
        
    </div>
</template>
<script>
    export default {
        data() {
            const validateData=(rule, value, callback) => {
                var temp=/^[0-9]\d*(\.\d+)?$/;//验证规则
                if(value!=null&&value!="")
                {
                    if(temp.test(value)==false)
                    {
                        callback(new Error('必须是大于0的数字'));
                    }
                    else{
                        callback();  
                    }
                }else{
                    callback();
                }
            };
            return {
                self: this,
                CreateYearList: this.GetGroupDict("ReportYear"), //周期
                CurrentYear: this.GetCurrentYear(),//当前年度
                BtnSaveStatus: false,//保存按钮开关
                isFull:false,
                //查询模型：根据需求删除不不要查询条件
                //蔬菜生产情况表代码
                searchModel: {
                    Id: '0',
                    ReportCode: 'VegExport',
                    FillInYear: '',
                    TimesType: '',
                    AreaCode: this.GetlocaStorage("VegUserInfo").AreaCode,
                    PageNumber: 1,
                    PageSize: 20,
                    Status: '',
                    Opinion: ''
                },
                DataModel:{//数据表代码
                    Id:'',
                    ReportId:'',
                    ProductNumber:'',//生产总量
                    SaleNumber:'',//销售量
                    ProductMoney:'',//
                    SaleMoney:'',
                    ReportStatus:'',                    
                    //********去年的数据********
                    OldId:'',
                    OldReportId:'',
                    OldProductNumber:'',//生产总量
                    OldSaleNumber:'',//销售量
                    OldProductMoney:'',//
                    OldSaleMoney:'',
                    OldReportStatus:''
                },
                ruleModel:{
                    ProductNumber: [{ validator: validateData, trigger: 'blur' }],
                    SaleNumber:[{ validator: validateData, trigger: 'blur' }],//销售量
                    ProductMoney:[{ validator: validateData, trigger: 'blur' }],//
                    SaleMoney:[{ validator: validateData, trigger: 'blur' }]
                },
                ctx: this, //table数据上下文
                clientHeight: window.innerHeight - 270, //表格默认高度
            }
        },
        mounted() {
            //table高度计算方法
            const that = this;
            window.onresize = () => {
                return (() => {
                    that.clientHeight = window.innerHeight - 220;
                })()
            };
        },
        methods: {
            checkProductData(){//检测生产数据平衡关系
                var ProductNumber=this.DataModel.ProductNumber;//生产总量
                var SaleNumber=this.DataModel.SaleNumber;//外销量
                if(SaleNumber!=null&&ProductNumber==null)
                {
                    return false;
                }
                else{
                    if(ProductNumber!=null&&SaleNumber!=null)
                    {
                        if(Number(ProductNumber)<=Number(SaleNumber))
                        {
                            return false;
                        }
                        else{
                            return  true;
                        }
                    }
                    else{
                        return true;
                    }
                }
                
            },
            checkSaleData(){//校验销售数据平衡关系
                var ProductMoney=this.DataModel.ProductMoney;
                var SaleMoney=this.DataModel.SaleMoney;
                if(SaleMoney!=null&&ProductMoney==null)
                {
                    return false;
                }
                else{
                    if(ProductMoney!=null&&SaleMoney!=null)
                    {
                        if(Number(ProductMoney)<=Number(SaleMoney))
                        {                       
                            return false;
                        }
                        else{
                            return  true;
                        }
                    }else{
                        return true;
                    }
                }
            },
            checkData:function(){//数据平衡关系校验
               if(this.checkProductData()&&this.checkSaleData())
               {
                   return true;
               }
               else{
                   return false;
               }
                
            },
            Import:function(ReportId,ReportCode){//数据导入
                var self=this;
                self.postExcel.post().then(function(res){
                    if(res.data.status===1){
                          self.$Notice.success({
                            title: res.data.message,
                            desc: false
                        });
                    }
                    else{
                          self.$Notice.success({
                            title: res.data.message,
                            desc: false
                        });
                    }
                });
            },
            TmpDown:function(){//模板下载
                window.open(this.$http.defaults.baseURL + "Report/GetTemplateData?ReportCode=" + this.searchModel.ReportCode);
            },
                //获取今往年数据
            GetData: function () {
                var self = this;
                var model = new Object();
                model.ReportCode = self.searchModel.ReportCode;
                if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
                    model.FillInYear = self.searchModel.FillInYear;
                    self.isFull= self.IsFillForTableYear(parseInt(self.searchModel.FillInYear));
                } else {
                    self.isFull= self.IsFillForTableYear(self.CurrentYear);
                    model.FillInYear = self.CurrentYear + '年';//填报年度-----数据字典值
                    self.searchModel.FillInYear = self.CurrentYear + '年';
                }
                model.AreaCode = self.searchModel.AreaCode;
                self.DataModel={};//清空当前数据            
                self.$http.post("Report/PostData", model).then( //获取数据编辑项的值
                    function (res) {
                        if (res.data.list.status == 2 || res.data.list.status == 1) {
                            self.DataModel = res.data.list.model;
                            self.BtnSaveStatus = true;
                        } else {
                            self.DataModel = res.data.list.model;
                            self.$Modal.warning({
                                title: "提示信息",
                                content: res.data.list.message
                            });
                            self.BtnSaveStatus = false;
                        }
                    }
                );
            },
            //保存方法
            save(name) {
                var self = this;
                this.$refs[name].validate((valid) => {
                    if (valid) 
                    {
                        if(self.checkData())
                        {
                            this.$http.put('VegExport', self.DataModel).then(
                            function(res) {
                                if (res.data.status == 1) {
                                    self.GetData();
                                    self.$Notice.success({
                                        title: '数据保存成功！',
                                        desc: false
                                    });
                                } else {
                                    self.$Modal.warning({
                                        title: "提示信息",
                                        content: "更新失败,请与管理员联系!"
                                    });
                                }
                            });
                        }
                        else{
                            self.$Modal.warning({
                                title: "提示信息",
                                content: "数据保存失败，请检查数据平衡关系是否正确!"
                            });
                        }
                    } else {
                        this.$Message.error('数据验证失败，请检查后再试!');
                    }
                })
            },
            //数据上报
            DataUp: function() {
                var self = this;
                var model = new Object();
                model.Id = self.DataModel.ReportId;
                model.Status = 3;
                model.Opinion = '';
                self.$Modal.confirm({
                    title: '提示信息',
                    content: '<p>数据上报后，不能再更改，是否继续上报？</p>',
                    onOk: () => {
                        self.$http.post('Report/PostUpStatus', model).then(function(res) {
                            if (res.data.status === 1) {
                                self.$Notice.success({
                                    title: '数据上报成功',
                                    desc: false
                                });
                                self.GetData(); //读取分页数据
                            } else {
                                self.$Notice.success({
                                    title: '数据上报失败',
                                    desc: res.data.messages
                                });
                            }
                        });
                    },
                    onCancel: () => {}
                });
            },
             //导出
            ExcelDown: function () {
                var self = this;
                var model = new Object();
                model.ReportCode = self.searchModel.ReportCode;
                if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
                    model.FillInYear = self.searchModel.FillInYear;
                } else {
                    model.FillInYear = self.CurrentYear + '年';//填报年度-----数据字典值
                    self.searchModel.FillInYear = self.CurrentYear + '年';
                }
                model.AreaCode = self.searchModel.AreaCode;
                window.open(this.$http.defaults.baseURL + "VegExport/GetExcelData?ReportCode=" + model.ReportCode + "&&FillInYear=" + model.FillInYear + "&&AreaCode=" + model.AreaCode);
            },
            //导入
            GetPostUrl() {
                var url = this.$http.defaults.baseURL + "Report/PostImportData?ReportCode=" + this.searchModel.ReportCode + "&&AreaCode=" + this.searchModel.AreaCode + "&&FillInYear=" + this.searchModel.FillInYear;
                return url;
            },
            handleSuccess(res, file) {
            var self = this;
            self.GetData();
            self.$Notice.success({
                title: '数据导入成功！',
                desc: false
            });
        },
        },
        //创建Vue页面方法
        created: function() {
            //创业Vue前调用获取this.datas: {},//table数据对象
            this.GetData();
        },
        watch: {
            //需要监听的在此处新增监听对象
        }
    }
</script>